package com.mrd.frame.service.impl;

import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mrd.frame.dao.AccessConfigDao;
import com.mrd.frame.dao.ConfigDao;
import com.mrd.frame.dao.WhiteListConfigDao;
import com.mrd.frame.domain.Config;
import com.mrd.frame.redis.JedisClientClusterImpl;
import com.mrd.frame.service.ConfigService;

@Service
public class ConfigServiceImpl implements ConfigService{
	@Autowired
    private ConfigDao configDao;
	@Autowired
	private AccessConfigDao accessConfigDao;
	@Autowired
	private WhiteListConfigDao whiteListConfigDao;
	@Autowired
	private JedisClientClusterImpl jedisClientClusterImpl;
	
	@Override
	public Config selectByUserKey(String userKey) {
		Config config = jedisClientClusterImpl.get("config_" + userKey, Config.class);
		if(config == null){
			config = configDao.selectByUserKey(userKey);
			jedisClientClusterImpl.set("config_" + userKey, config, null, TimeUnit.MILLISECONDS);
		}
		return config;
	}

	@Override
	public int insert(Config record) {
		int i = 0;
		if(record != null){
			i += configDao.insert(record);
			if(record.getAccessConfigList() != null){
				i += accessConfigDao.insertBatch(record.getAccessConfigList());
			}
			if(record.getWhiteListConfigList() != null){
				i += whiteListConfigDao.insertBatch(record.getWhiteListConfigList());
			}
		}
		return i;
	}

	@Override
	public int deleteByPrimaryKey(String configId) {
		return configDao.deleteByPrimaryKey(configId);
	}

	@Override
	public int insertSelective(Config record) {
		return configDao.insertSelective(record);
	}

	@Override
	public Config selectByPrimaryKey(String configId) {
		return configDao.selectByPrimaryKey(configId);
	}

	@Override
	public int updateByPrimaryKeySelective(Config record) {
		return configDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Config record) {
		return configDao.updateByPrimaryKey(record);
	}

}
